package com.zc.commons.utils;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

/**
 * 资源工具类
 *
 * @author zcj
 * @version 1.0.0
 * @date 2022/7/6 11:39
 */
public class ResourceUtils {
    /**
     * jar包
     */
    public static final String JAR = "jar";

    public static final InputStream getResourceInputStream(ClassLoader classLoader, String resourcePath) throws IOException {
        URL url = classLoader.getResource("");
        resourcePath = (JAR.equals(url.getProtocol()) ? "/" : "") + resourcePath;
        return classLoader.getResource(resourcePath).openStream();
    }

    /**
     * @param clz
     * @param resourcePath
     * @return
     * @throws IOException
     */
    public static final InputStream getResourceInputStream(Class clz, String resourcePath) throws IOException {
        return getResourceInputStream(clz.getClassLoader(), resourcePath);
    }

}
